﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/App.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import Namespace="Ornament.NHibernates.Configurations" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Nhibernate 设定
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="HeadContent" runat="server">

    <script src="/Scripts/jquery.bindTools.js" type="text/javascript"></script>

    <script src="/Scripts/jquery.json.min.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">
        var options = {
            bindWay: [
                    { member: "Runtime.Timeout", to: "#timeout" },
                    { member: "DriverInfo.Dialect", to: "#Dialect" },
                    { member: "DriverInfo.Dialect", to: "#DropDownlistListDataBaseTypes" },
                    { member: "DriverInfo.DriverClass", to: "#SuggestionDriverClassDropDonwList",
                        onBind:
                        function() {
                            $("#DropDownlistListDataBaseTypes").change();
                        }
                    },
                    { member: "DriverInfo.ConnectionInConfig", to: "#UseConfigFileConnection", onBound: UseConfigFileConnection_click },
                    { member: "DriverInfo.ConnectionString", to: "#connectionString" },
                    { member: "DriverInfo.ConnectionStringKey",
                        to: function(value) {
                            if (value != "") {
                                $("option:contains('" + value + "')", "#webConfigConnection").attr("selected", "selected");
                            }
                        }, from: function(obj) {
                            obj.DriverInfo.ConnectionStringKey = $("option[selected]", "#webConfigConnection").text();
                        }
                    },
                    { member: "Runtime.BatchSize", to: "#batchSize" },
                    { member: "Runtime.ShowSql", to: "#ShowSql" },

                    { member: "Runtime.UseOuterJoin", to: "#UseOuterJoin" },
                    { member: "Runtime.Isolation", to: "#DropDownlistListIsolation" },
                    { member: "Mappings", to: mappingBindTo, from: mappingBindFrom }
                ],
            get: {
                url: '<%=Url.Action("GetseesionFactroy")%>'
            }
        };
        function mappingBindTo(data) {
            for (var i = 0; i < data.length; i++) {
                var id = data[i].Assembly.replace(/\./g, "_");
                $("#" + id).attr("checked", "checked");
            }
        }
        function mappingBindFrom(jsonObject) {
            jsonObject.Mappings = new Array();
            $("#mappingList li input:checked").each(function() {
                var mapp = {
                    Assembly: $(this).val()
                }
                jsonObject.Mappings.push(mapp);
            });
        }

        var sessionFactory;
        $(document).ready(function() {

            $('#nhibernateSetting').tabs().bindTool(options);

            $("#configurationFile").change(function() {
                location = "/settings/nhibernate/index" + this.selectedIndex;
            });

            $("#CreateTable").click(function() {
                if (confirm("此操作会把所有数据删除并重新初始化系统，是否继续？")) {
                    var actionUrl = '<%=Url.Action("CreateTable") %>';
                    $.getJSON(actionUrl, function(data) {
                        alert(data.Message);
                    });
                }
            });

            $("#Save").click(function() {
                var actionUrl = '<%=Url.Action("Save") %>';
                var data1 = { filePath: $("#configurationFile").val(), sessionFactoryName: $("#SectionFactoryNames").val() };
                var obj = $("#nhibernateSetting").bindTool('collect');
                data1.sessionFactory = $.toJSON(obj);
                $.post(actionUrl, data1, function(data) { alert(data.Message) }, "json");
            });

            $("#ReloadSessionFactory").click(function() { LoadSessionFactory(); alert("成功重置", "成功"); });

            $("#SectionFactoryNames").change(LoadSessionFactory).change();

            //driverInfo
            $("#UseConfigFileConnection").click(UseConfigFileConnection_click);
            $("#webConfigConnection").change(function() {
                $("#connectionString").val($(this).val());
            });


        });

        function LoadSessionFactory() {
            var data = { filePath: $("#configurationFile").val(), sessionfactoryName: $("#SectionFactoryNames").val() };
            $("#nhibernateSetting").bindTool('bind', data);
        }

        function UseConfigFileConnection_click() {

            if ($("#UseConfigFileConnection")[0].checked) {
                $("#webConfigConnection").removeAttr("disabled");
            }
            else {
                $("#webConfigConnection").attr("disabled", "disabled");
            }
        }
    </script>

</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    Configuration Files:
    <%=Html.DropDownList("configurationFile", (IEnumerable<SelectListItem>)ViewData["configFiles"])%>
    <div>
        Session Factory Name:
        <%=Html.DropDownList("SectionFactoryNames", (IEnumerable<SelectListItem>)ViewData["sessionFactoryName"])%>
        <input type="button" id="ReloadSessionFactory" value="重置" class="ui-state-default ui-corner-all" />
    </div>
    <input id="Save" value="保存" type="button" />
    <input id="CreateTable" value="重新生成数据表" type="button" title="根据Mapping的信息,重建NHibernate管理下的相关数据表,数据将会被清除" />
    <div id="nhibernateSetting">
        <ul>
            <li><a href="#DriverType">DriverType</a></li>
            <li><a href="#runtime">Runtime</a></li>
            <li><a href="#mapping">Mapping</a></li>
        </ul>
        <div id="DriverType">
            <%Html.RenderPartial("DriverInfo"); %>
        </div>
        <div id="runtime">
            <%Html.RenderPartial("RuntimeInfo"); %>
        </div>
        <div id="mapping">
            <%Html.RenderPartial("MappingList"); %>
        </div>
    </div>
</asp:Content>
